<?php
/**
 * 
 * @author chenlisong E-mail:chenlisong1021@163.com 
 * @version 创建时间：2016年12月5日 下午4:38:25 
 * 类说明
 */
class LoginAction extends BaseAction{ 
	
	Public function index(){
		if(isset($_SESSION['HS_USER_ID'])){
			redirect(__ROOT__);
		}
		$this->display();
	}
	
	/**
	 * 登录
	 */
	public function login(){
		$User = M('User');
		$param = $this->param;
		if(empty($param['password']) || empty($param['username'])){
			$this->ajaxReturn(0,"参数错误！",0);
		}
		$where['username|tel'] = $param['username'];
		$where['flag'] = 1;
		$info = $User->where($where)->find();
		if(count($info) == 0){
			$this->ajaxReturn(0,"未找到该用户！",0);
		}
		if($info['password'] != md5($param['password'])){
			$this->ajaxReturn(0,"用户名或密码不正确！",0);
		}
		$_SESSION['HS_USER_ID'] = $info['id'];
		$_SESSION['HS_USER_TEL'] = $info['tel'];
		$this->ajaxReturn(1,'登录成功！',1);
	}
	
	/**
	 * 注册
	 */
	Public function register(){
		$this->display();
	}
	
	/**
	 * 注册
	 * 取消表单自动添加
	 */
	public function registerUser(){
		$User = M("User");
		$param = $this->param;
		if(!checkPhone($param['tel'])){
			$this->ajaxReturn(0,"错误的手机格式！",0);
		}
		if(empty($param['tel_code'])){
			$this->ajaxReturn(0,'验证码为空！',0);
		}
		if($param['tel'] != session('send_code_tel')){
			$this->ajaxReturn(0,"请重新获取验证码！",0);
		}
		if($param['tel_code'] != session('send_tel_code')){
			$this->ajaxReturn(0,"验证码错误！",0);
		}
		//验证手机号唯一
		$is_ex = $User->where('tel = "'.$param['tel'].'" and flag = 1 ')->count();
		if($is_ex > 0){
			$this->ajaxReturn(0,"该手机号码已注册，可直接登录！",0);
		}
		//判断推荐人是否存在
		if(!empty($param['parent_id'])){
			$count = $User->where('flag = 1 and id = '.$param['parent_id'])->count();
			if($count == 0){
				$this->ajaxReturn(0,'推荐人ID不存在！',0);
			}
		}
		$data['username'] = $param['tel'];
		$data['password'] = md5($param['password']);//客户端加密
		$data['tel'] = $param['tel'];
		$data['parent_id'] = $param['parent_id'];
		$data['create_time'] = time();//获取当前时间戳
		$lastInsId  = $User->data($data)->add();
		// 写入数据
		if($lastInsId){
			//清空手机验证码的缓存时间和验证码
			session("send_code_time",null);
			session('send_code_tel',null);
			session('send_tel_code',null);
			//自动登录
			$_SESSION['HS_USER_ID'] = $lastInsId;
			$_SESSION['HS_USER_TEL'] = $param['tel'];
			$this->ajaxReturn(1,'注册成功！',1);
		} else {
			$this->ajaxReturn(0,'服务器穿越了~请稍后再试~',0);
		}
	}
	
	/**
	 * 找回密码
	 */
	public function findPassword(){
		$this->display();
	}
	
	/**
	 * 找回密码FUN
	 */
	public function updatePassword(){
		$User = M('User');
		$param = $this->param;
		if(!checkPhone($param['tel'])){
			$this->ajaxReturn(0,"错误的手机格式！",0);
		}
		if(empty($param['tel_code'])){
			$this->ajaxReturn(0,'验证码为空！',0);
		}
		if($param['tel'] != session('send_code_tel')){
			$this->ajaxReturn(0,"请重新获取验证码！",0);
		}
		if($param['tel_code'] != session('send_tel_code')){
			$this->ajaxReturn(0,"验证码错误！",0);
		}
		$where['flag'] = 1;
		$where['tel'] = $param['tel'];
		$info = $User->where($where)->find();
		if(empty($info)){
			$this->ajaxReturn(0,"未找到该用户！",0);
		}
		$data['password'] = md5($param['password']);
		$data['update_time'] = time();
		$result = $User->where($where)->save($data);
		if($result !== false){
			session("send_code_time",null);
			session('send_code_tel',null);
			session('send_tel_code',null);
			$this->ajaxReturn(1,'修改成功！',1);
		}else {
			$this->ajaxReturn(0,"修改失败！",0);
		}
	}
	
	/**
	 * 修改密码成功
	 */
	public function success(){
		$this->display();
	}
	
	/**
	 * 退出
	 */
	public function logout(){
		//清除前台的服务端的session值
		$_SESSION['HS_USER_ID'] = NULL;
		$_SESSION['HS_USER_TEL'] = NULL;
		session(null);
		header("Location: ".$_SERVER['HTTP_REFERER']);
	}
	
	/**
	 * 发送短信
	 */
	public function sendPhoneCode(){
		$param = $this->param;
		$tel = $param['tel'];
		if(!checkPhone($tel)){
			$this->ajaxReturn(0,"错误的手机号！",0);
		}
		$tel_code = rand(100001,999999);
		if((session("send_code_time") + 60) > time() && isset($_SESSION['send_code_time'])) {//将获取的缓存时间转换成时间戳加上60秒后与当前时间比较，小于当前时间为频繁获取
			$this->ajaxReturn(0,"请勿频繁获取验证码！",0);
		}else{
			import('COM.Core.SendMsg');
			$SendMsg = new SendMsg();
			$content = '您的验证码是['.$tel_code.']';
			$_SESSION['send_tel_code'] = $tel_code;
			$result = $SendMsg->sendMsg($tel, $content);
			session('send_code_time',time()); // 发送成功后记录当前发送的时间
			session('send_code_tel',$tel);
			$this->ajaxReturn(1,'发送成功！',1);
		}
	}
	
	/**
	 * 检查手机是否可注册
	 */
	public function checkTel(){
		$User = M('User');
		$tel = $_REQUEST['param'];
		$where['tel'] = $tel;
		$where['flag'] = 1;
		$count = $User->where($where)->count();
		if($count != 0){
			echo '用户手机已被注册！';
		}else{
			echo 'y';
		}
	}
	
	/**
	 * 检查手机是否已注册
	 * 用于检测是否可找回密码
	 */
	public function checkTel2(){
		$User = M('User');
		$tel = $_REQUEST['param'];
		$where['tel'] = $tel;
		$where['flag'] = 1;
		$count = $User->where($where)->count();
		if($count == 0){
			echo '不存在的用户！';
		}else{
			echo 'y';
		}
	}
}
